import { StringUtils } from '../../../../utils/StringUtils'
import { computed, reactive } from 'vue'

export const PAGE_ENTRY_ARG = {
  M_IN: 1,
  M_OUT: 2,

  M_REFUND: 9,

  P_IN: 5,
  P_OUT: 6,
  P_REFUND: 10,

  O_IN: 7,
  O_OUT: 8
}

export const is_out = function(value) {
  return [
    PAGE_ENTRY_ARG.M_OUT,
    PAGE_ENTRY_ARG.P_OUT,
    PAGE_ENTRY_ARG.M_REFUND
  ].includes(value)
}

export const is_in = function(value) {
  return [
    PAGE_ENTRY_ARG.M_IN,
    PAGE_ENTRY_ARG.P_IN,
    PAGE_ENTRY_ARG.P_REFUND
  ].includes(value)
}

export const is_o_out = function(value) {
  return [
    PAGE_ENTRY_ARG.O_OUT
  ].includes(value)
}

export const is_o_in = function(value) {
  return [
    PAGE_ENTRY_ARG.O_IN
  ].includes(value)
}

export const PAGE_PARAM = {
  [PAGE_ENTRY_ARG.M_IN]: { title: '原材料', BUSS_TYPE: 31 },
  [PAGE_ENTRY_ARG.M_OUT]: { title: '原材料', BUSS_TYPE: 51 },
  [PAGE_ENTRY_ARG.M_REFUND]: { title: '原材料退货', BUSS_TYPE: 54 },

  [PAGE_ENTRY_ARG.P_IN]: { title: '成品', BUSS_TYPE: 32 },
  [PAGE_ENTRY_ARG.P_OUT]: { title: '成品', BUSS_TYPE: 52 },
  [PAGE_ENTRY_ARG.P_REFUND]: { title: '成品退货', BUSS_TYPE: 33 },

  [PAGE_ENTRY_ARG.O_IN]: { title: '其他入库', BUSS_TYPE: 34 },
  [PAGE_ENTRY_ARG.O_OUT]: { title: '其他出库', BUSS_TYPE: 53 }
}

export const getPageTypeName = function(PAGE_ENTRY_ARG) {
  return PAGE_PARAM[PAGE_ENTRY_ARG]['title']
}

export const setPageTitle = function(page_param, info = '') {
  const { __PAGE_ID } = page_param || {}

  let title = getPageTypeName(__PAGE_ID)

  if (is_out(__PAGE_ID)) {
    title += '出库'
  } else if (is_in(__PAGE_ID)) {
    title += '入库'
  }
  let t = title + info
  if (!StringUtils.isEmptyOrNull(info)) {
    uni.setNavigationBarTitle({
      title: t
    })
  }


  return t
}


export const usePageParamHook = function() {
  const PAGE_HOOK_PARAM = reactive({
    PAGE_TYPE_NAME: '',
    API_TYPE: '',
    BUSS_TYPE: ''
  })

  const setupPage = function(params = {}) {
    let _p = params || {}
    Object.entries(_p).map(([key, value]) => {
      PAGE_HOOK_PARAM[key] = value
    })

    setPageTitle(_p, '单')
    PAGE_HOOK_PARAM.PAGE_TYPE_NAME = setPageTitle(_p)
    const { __PAGE_ID } = PAGE_HOOK_PARAM || {}

    PAGE_HOOK_PARAM.BUSS_TYPE = PAGE_PARAM[__PAGE_ID]['BUSS_TYPE']
    if (is_in(__PAGE_ID)) {
      PAGE_HOOK_PARAM.API_TYPE = 'in'
    } else if (is_out(__PAGE_ID)) {
      PAGE_HOOK_PARAM.API_TYPE = 'out'
    } else if (is_o_out(__PAGE_ID)) {
      PAGE_HOOK_PARAM.API_TYPE = 'otherout'
    } else if (is_o_in(__PAGE_ID)) {
      PAGE_HOOK_PARAM.API_TYPE = 'otherin'
    }
  }

  const getButtonTitle = computed(() => {
    const { __PAGE_ID } = PAGE_HOOK_PARAM || {}
    let title = ''
    if (is_out(__PAGE_ID) || is_o_out(__PAGE_ID)) {
      title = '出库'
    } else if (is_in(__PAGE_ID) || is_o_in(__PAGE_ID)) {
      title = '入库'
    }

    return title
  })

  return {
    setupPage,
    getButtonTitle,
    PAGE_HOOK_PARAM
  }
}
